"""
Default mapp analysis settings.
"""
from os.path import join
from django.conf import settings
import os

def check_create_dir(varname, dirpath):
    if not os.path.isdir(dirpath):
        try:
            os.mkdir(dirpath)
        except OSError as e:
            raise ValueError("Settings '%s': directory '%s' could not be created!\n%s" % (varname, dirpath, str(e)))

MAPP_PVALUE = getattr(settings, 'MAPP_PVALUE', 0.1)

#MAPP files directories
MAPP_APP_DIR = getattr(settings, 'MAPP_APP_DIR', os.path.dirname(os.path.abspath(__file__)))
MAPP_WORK_DIR = getattr(settings, 'MAPP_WORK_DIR', join(MAPP_APP_DIR, "files"))
MAPP_RESULT_DIR = getattr(settings, 'MAPP_RESULT_DIR', MAPP_WORK_DIR)
MAPP_SEQUENCE_DIR = getattr(settings, 'MAPP_SEQUENCE_DIR', MAPP_WORK_DIR)
MAPP_MUTS_DIR = getattr(settings, 'MAPP_MUTS_DIR', MAPP_WORK_DIR)
MAPP_PROG_DIR = getattr(settings, 'MAPP_PROG_DIR', join(MAPP_APP_DIR, "programs"))

#check MAPP files directories
check_create_dir('MAPP_WORK_DIR', MAPP_WORK_DIR)
check_create_dir('MAPP_RESULT_DIR', MAPP_RESULT_DIR)
check_create_dir('MAPP_SEQUENCE_DIR', MAPP_SEQUENCE_DIR)
check_create_dir('MAPP_PROG_DIR', MAPP_PROG_DIR)
check_create_dir('MAPP_MUTS_DIR', MAPP_MUTS_DIR)

#name patterns of created files
MAPP_SEQ_FILE = getattr(settings, 'SEQ_FILE', join(MAPP_SEQUENCE_DIR, "{id}.seq"))
MAPP_BLAST_FILE = getattr(settings, 'BLAST_FILE', join(MAPP_WORK_DIR, "{id}.fa"))
MAPP_MSA_FILE = getattr(settings, 'MSA_FILE', join(MAPP_WORK_DIR, "{id}.afa"))
MAPP_TREE_FILE = getattr(settings, 'TREE_FILE', join(MAPP_WORK_DIR, "{id}.nwk"))
MAPP_FILE = getattr(settings, 'MAPP_FILE', join(MAPP_RESULT_DIR, "{id}.mapp"))
MAPP_STAT_FILE = getattr(settings, 'STAT_FILE', join(MAPP_WORK_DIR, "{id}.stats"))
MAPP_MUTS_FILE = getattr(settings, 'MAPP_MUTS_FILE', join(MAPP_MUTS_DIR, "{id}.muts"))

#single program commands
MAPP_MAFFT = join(MAPP_PROG_DIR, 'runmafft.sh {blastout} {msaout}')
MAPP_CLUSTALO = join(MAPP_PROG_DIR, 'runclustalo.sh {blastout} {msaout}')
MAPP_FASTTREE = join(MAPP_PROG_DIR, 'runfasttree.sh {msaout} {treeout}.crude')
MAPP_RAXML = join(MAPP_PROG_DIR, 'runraxml.sh {msaout} {treeout}.crude')
#program paths
#You can use reference to the files. Names of the references you can
#find on mapp.core.parsers.BasicParser constatns.
#E.g. MAPP_PROGRAMS['BLAST']="blastp -i {sequence} -o {blastout}"
#{sequece} and {blastout} will substitute for real sequence file name
#and real blastout name. Filename will be putted to
#the MAPP_PROGRAMS['BLAST'].format() method
MAPP_PROGRAMS = getattr(settings, 'MAPP_PROGRAMS', dict())
MAPP_PROGRAMS.setdefault('BLAST', join(MAPP_PROG_DIR, 'runhmmer_remote.sh {sequence} {blastout} {initseqs} {clusters} {blaststat}'))
MAPP_PROGRAMS.setdefault('BEFOREMSA', join(MAPP_PROG_DIR, 'adjust_blastout.sh {blastout} {sequence} {clusters}'))
MAPP_PROGRAMS.setdefault('MSA', MAPP_MAFFT)
MAPP_PROGRAMS.setdefault('BEFORETREE', '')
MAPP_PROGRAMS.setdefault('TREE', MAPP_FASTTREE)
MAPP_PROGRAMS.setdefault('BEFOREMAPP', join(MAPP_PROG_DIR, 'adjustnewick.sh {treeout}.crude {treeout}'))
MAPP_PROGRAMS.setdefault('MAPP', join(MAPP_PROG_DIR, 'runmapp.sh {msaout} {treeout} {mappout}'))



